
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Authorization;
using Yozeev.Common;
using Microsoft.AspNetCore.Mvc;

namespace Yozeev.BusinessLogic
{
    /// <summary>
    /// TravelAgency应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TravelAgencyAppService : YozeevAppServiceBase, ITravelAgencyAppService
	{
		private readonly IRepository<TravelAgency, Guid> _entityRepository;
		private readonly IRepository<RechargeRecord, Guid> _rechargeRepository;

		private readonly ITravelAgencyManager _entityManager;

		private readonly IQueryHelper _queryHelper;
		/// <summary>
		/// 构造函数 
		///</summary>
		public TravelAgencyAppService(
        IRepository<TravelAgency, Guid> entityRepository
        ,ITravelAgencyManager entityManager
			, IQueryHelper queryHelper
			, IRepository<RechargeRecord, Guid> rechargeRepository
		)
        {
            _entityRepository = entityRepository; 
            _entityManager=entityManager;
			_queryHelper = queryHelper;
			_rechargeRepository = rechargeRepository;

		}


        /// <summary>
        /// 获取TravelAgency的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
		[HttpPost]
        public async Task<PagedResultDto<TravelAgencyListDto>> GetPaged(GetTravelAgencysInput input)
		{

		    var query = _entityRepository.GetAll();
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			// var entityListDtos = ObjectMapper.Map<List<TravelAgencyListDto>>(entityList);
			var entityListDtos =entityList.MapTo<List<TravelAgencyListDto>>();

			return new PagedResultDto<TravelAgencyListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TravelAgencyListDto信息
		/// </summary>
		public async Task<TravelAgencyListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<TravelAgencyListDto>();
		}

		/// <summary>
		/// 获取编辑 TravelAgency
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetTravelAgencyForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetTravelAgencyForEditOutput();
TravelAgencyEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<TravelAgencyEditDto>();

				//travelAgencyEditDto = ObjectMapper.Map<List<travelAgencyEditDto>>(entity);
			}
			else
			{
				editDto = new TravelAgencyEditDto();
			}

			output.TravelAgency = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TravelAgency的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateTravelAgencyInput input)
		{

			if (input.TravelAgency.Id.HasValue)
			{
				await Update(input.TravelAgency);
			}
			else
			{
				await Create(input.TravelAgency);
			}
		}


		/// <summary>
		/// 新增TravelAgency
		/// </summary>
		protected virtual async Task<TravelAgencyEditDto> Create(TravelAgencyEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <TravelAgency>(input);
            var entity=input.MapTo<TravelAgency>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<TravelAgencyEditDto>();
		}

		/// <summary>
		/// 编辑TravelAgency
		/// </summary>
		protected virtual async Task Update(TravelAgencyEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TravelAgency信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TravelAgency的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出TravelAgency为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}


		/// <summary>
		/// 获取TravelAgency的分页列表信息--Post方法
		///</summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<PagedResultDto<TravelAgencyListDto>> GetPagedForPost(GetTravelAgencysInput input)
		{
			var expression = _queryHelper.WhereExp<TravelAgency>(input.queryData);
			var query = _entityRepository.GetAll().Where(expression);

			// TODO:根据传入的参数添加过滤条件
			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();
			var entityListDtos = entityList.MapTo<List<TravelAgencyListDto>>();
			return new PagedResultDto<TravelAgencyListDto>(count, entityListDtos);
		}
		/// <summary>
		/// 旅行社充值接口
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task Recharge(CreateOrUpdateRechargeRecordInput input)
		{
			//创建充值记录
			RechargeRecord rechargeRecord = input.RechargeRecord.MapTo<RechargeRecord>();
			rechargeRecord.PayMethodType = Abp.Domain.Entities.Auditing.PayMethodEnum.PaySuccess;
			rechargeRecord.ComsuneStatus = Abp.Domain.Entities.Auditing.ComsuneStatusEnum.RechargeRecord;
			await _rechargeRepository.InsertAndGetIdAsync(rechargeRecord);
			//更新旅行社预存金额
			var travel = await _entityRepository.GetAsync(input.RechargeRecord.TravelAgencyId);
			travel.PrepaidAmount += input.RechargeRecord.Amount;
			travel.TotalSum += input.RechargeRecord.Amount;
			await _entityRepository.UpdateAsync(travel);
		}
	}
}


